/**
 * @author mehdi
 */
 /*
  * dialogue instrument
  */
 
 /*
  * variable globale
  */
 var instrumentSelected = "Instrument1"; // mettre l'instrument de la partition
 var instrumentChoice= "Instrument1";
 var id_user= "Instrument1";
 
 /*
  * events
  */
 afficheInfos();
    runDialogInstrument = function() {
    	
	    $( "#dialogInstrument" ).dialog({
			resizable: true,
			width: '600px',
	      	buttons: {
	        "Valider": function() {
	        	instrumentSelected = instrumentChoice;
				user_id = instrumentChoice.substring(10,20);
	        	window.location = "partition.php?partId="+partId+"&numPage=1&user="+user_id;
	          $( this ).dialog( "close" );
	        }
	      }
	    });
	    
	     var instDivSelected = document.getElementById(instrumentSelected);
	     SelectInstrument(instDivSelected);
	  };
	  
	  var SelectInstrument = function(instrument){
	  	instrumentChoice = instrument.id;
	  	id_user = instrument.id;
	  	instrument.style.backgroundImage= 'linear-gradient( #A2B5BF 0%, #5F8CA3 50%)';
	  	instrument.style.color = 'black';
	  	instrument.style.border = '5px double white';
	  	instrument.style.margin = '5px 15px 5px 15px';

	    var instruments = document.getElementsByClassName('instrument');
	    for (var i = 0, c = instruments.length; i < c; i++) {
	    	if(instrumentChoice != instruments[i].id)
	    	{
			  	instruments[i].style.backgroundImage= 'linear-gradient( #515151 0%, #000000 50%)';
			  	instruments[i].style.color = 'white';
			  	instruments[i].style.border = '10px';
			  	instruments[i].style.margin = '10px 20px 10px 20px';	    		
	    	}		  	      
	    }

	  }
	  
 function afficheInfos(){
			var partid=getQuerystring('partId');
			var request = $.ajax({
				type: "POST",
				url: "./ajax/partUserList.php",
				data: { id: partid },
				dataType: "json"
			});
			request.done(function( datas ) {
				$('#listeInstrument div').remove();
				$.each(datas.users,function(i,user){
					var msg = "<div id=instrument"+user.id+" class='instrument' onclick='SelectInstrument(instrument"+user.id+")'>"
					msg+= "<p>"+user.UICName+"</p></div>"
    
				   $('#listeInstrument').append(msg);
				 });

			}); //end done
			
		
  }
  
   function getQuerystring(key, default_) {
       if (default_==null) default_="";
       key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
       var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
       var qs = regex.exec(window.location.href);
       if(qs == null) return default_; else return qs[1];
   }